package portal.web.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import portal.vo.ProductoVO;
import portal.web.businessDelegate.PortalComprasDelegate;
import portal.web.utils.EncodingUtil;

@SuppressWarnings("serial")
public class BuscadorServlet extends HttpServlet {

	private static Logger logger = Logger.getLogger(BuscadorServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>BuscadorServlet<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		
		String nombre = EncodingUtil.fixEncoding(req.getParameter("busqueda"));
		
		System.out.println("Busqueda: " + nombre);
		
		List<ProductoVO> listado = new ArrayList<ProductoVO>();
		
		listado = PortalComprasDelegate.getInstance().getListaProductosByNombre(nombre);

		req.getSession().setAttribute("listaProductos", listado);
		
		req.getRequestDispatcher("/jsp/listaDeProductos.jsp").forward(req, resp);

	}
}
